package webservice;

import javax.json.Json;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import util.Teste;

/**
 * Trabalho PD - 2014/2015
 *
 * @author a21210380 a21210392
 */
@Path("/RestFulTeste")
public class RestFulTeste {

    static Teste teste = new Teste();

    @GET
    @Path("/testes")
    @Produces(MediaType.APPLICATION_JSON)
    public String getTestes() {
        return Json.createObjectBuilder().add("testes", teste.toJson()).build().toString();
    }

    @POST
    @Path("/testes/name={name}&age={age}&address={address}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String insertTeste(@PathParam("name") String name, @PathParam("age") int age, @PathParam("address") String address) {
        return "{\"success\":" + (teste.addUser(name, age, address) ? "true" : "false") + "}";
    }

    @PUT
    @Path("/testes/id={id}&name={name}&age={age}&address={address}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String updateTeste(@PathParam("id") int id, @PathParam("name") String name, @PathParam("age") int age, @PathParam("address") String address) {
        return "{\"success\":" + (teste.editUser(id, name, age, address) ? "true" : "false") + "}";
    }

    @DELETE
    @Path("/testes/id={id}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public String updateTeste(@PathParam("id") int id) {
        return "{\"success\":" + (teste.removeUser(id) ? "true" : "false") + "}";
    }
}
